package com.kira.scaffoldmvc.Controller;

import cn.hutool.json.JSONUtil;
import com.kira.scaffoldmvc.POJO.*;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@Slf4j
@RestController
@RequestMapping("/user")
public class StoreItController {



    @PostMapping("/logout")
    public LoginOrRegistry logout(HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");

            // 创建HttpClient实例
            HttpClient client = HttpClient.newHttpClient();

            // 创建HttpRequest实例，设置请求URL和请求方式
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("http://47.119.148.251:7000/user/logout"))
                    .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                    .POST(HttpRequest.BodyPublishers.noBody())
                    .build();

            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            LoginOrRegistry loginResponse = JSONUtil.toBean(response.body(), LoginOrRegistry.class);

            return loginResponse;

    }

    @GetMapping("/userInfo")
    public GetUserInfo userInfo(HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 从 HttpServletRequest 中获取 Authorization 头信息
        String authorizationHeader = httpServletRequest.getHeader("Authorization");


            // 创建HttpClient实例
            HttpClient client = HttpClient.newHttpClient();

            // 创建HttpRequest实例，设置请求URL和请求方式
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("http://47.119.148.251:7000/user/userInfo"))
                    .header("Authorization", authorizationHeader != null ? authorizationHeader : "")
                    .GET()
                    .build();

            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            log.info(response.body());
            // 处理响应
            System.out.println("响应状态码: " + response.statusCode());
            System.out.println("响应体内容: " + response.body());
            GetUserInfo loginResponse = JSONUtil.toBean(response.body(), GetUserInfo.class);

            return loginResponse;

    }






    @PostMapping("/getLogiOtp")
    public LoginOrRegistry login(@RequestBody GetLogiOtp getLogiOtp) throws IOException, InterruptedException {
        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();


        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(getLogiOtp);

        log.info("这是我们的请求体body的参数:{}",jsonBody);
        // 创建HttpRequest实例，设置请求URL、请求方式和请求体
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/user/getLogiOtp"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        LoginOrRegistry loginResponse = JSONUtil.toBean(response.body(), LoginOrRegistry.class);
        return loginResponse;


    }



    @PostMapping("/login")
    public LoginOrRegistry login(@RequestBody Login login, HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(login);

        log.info("请求体body信息:{}",jsonBody);
        // 创建HttpRequest实例，设置请求URL、请求方式和请求体
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/user/login"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody));

//        // 获取原始请求头并添加到新请求中
//        java.util.Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String headerName = headerNames.nextElement();
//            String headerValue = httpServletRequest.getHeader(headerName);
//            // 避免覆盖已设置的 Content-Type 头
//            if (!"Content-Type".equalsIgnoreCase(headerName)) {
//                requestBuilder.header(headerName, headerValue);
//            }
//        }

        HttpRequest request = requestBuilder.build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        // 将响应体转换为 LoginOrRegistry 对象
        LoginOrRegistry loginOrRegistry = JSONUtil.toBean(response.body(), LoginOrRegistry.class);

        return loginOrRegistry;
    }


    @PostMapping("/register")
    public LoginOrRegistry register(@RequestBody Register register, HttpServletRequest httpServletRequest) throws IOException, InterruptedException {
        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 使用Hutool的JSONUtil将对象转换为JSON字符串
        String jsonBody = JSONUtil.toJsonStr(register);
        System.out.println("请求体JSON字符串: " + jsonBody);

        // 创建HttpRequest实例，设置请求URL、请求方式和请求体
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create("http://47.119.148.251:7000/user/register"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody));

//        // 获取原始请求头并添加到新请求中
//        java.util.Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String headerName = headerNames.nextElement();
//            String headerValue = httpServletRequest.getHeader(headerName);
//            // 避免覆盖已设置的 Content-Type 头
//            if (!"Content-Type".equalsIgnoreCase(headerName)) {
//                requestBuilder.header(headerName, headerValue);
//            }
//        }

        HttpRequest request = requestBuilder.build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        // 处理响应
        System.out.println("响应状态码: " + response.statusCode());
        System.out.println("响应体内容: " + response.body());
        LoginOrRegistry loginOrRegistry = JSONUtil.toBean(response.body(), LoginOrRegistry.class);
        return loginOrRegistry;
    }




}
